﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HDEC.Controls
{
    /// <summary>
    /// RstnProgress.xaml 的交互逻辑
    /// </summary>
    public partial class DoubleCompletionBar : Window
    {
        private static DoubleCompletionBar rstnDlg = null;
        public static bool IsAbort = false; //执行过程中是否终止了操作
        public static bool IsRunning = false;//进度条是否正在运行
        public static bool IsAutoIncrease = false;//大进度条自动推进
        public static int TaskCount = 1; //任务数

        private bool _IsShowDoubleProgress = false;
        private bool _IsEnableAbort = false;
        private delegate void UpdateProgressDelegate(System.Windows.DependencyProperty dp1, Object value1);
        internal bool IsShowDoubleProgress
        {
            get { return _IsShowDoubleProgress; }
            set
            {
                _IsShowDoubleProgress = value;
                InitializeProgress();
            }
        }

        internal bool IsEnableAbort
        {
            get { return _IsEnableAbort; }
            set
            {
                _IsEnableAbort = value;
                InitializeProgress();
            }
        }

        internal DoubleCompletionBar()
        {
            InitializeComponent();
            _IsEnableAbort = false;
            _IsShowDoubleProgress = false;
            InitializeProgress();

            this.Topmost = true;
            //wndHelper = new WPFInteropHelper(this);
            //wndHelper.Attach(null, false, "");

        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }

        private new void  Show()
        {
            IsRunning = true;
            IsAbort = false;
            base.Show();
        }

        private void InitializeProgress()
        {
            if (IsEnableAbort)
                this.btnAbort.Visibility = Visibility.Visible;
            else
                this.btnAbort.Visibility = Visibility.Collapsed;

            if (IsShowDoubleProgress)
            {
                this.lblMainInfo.Visibility = Visibility.Visible;
                this.progressBar1.Visibility = Visibility.Visible;
            }
            else
            {
                this.lblMainInfo.Visibility = Visibility.Collapsed;
                this.progressBar1.Visibility = Visibility.Collapsed;
            }
        }

        private void btnAbort_Click(object sender, RoutedEventArgs e)
        {
            if(MessageBoxResult.Yes==MessageBox.Show("中止当前操作?","取消",MessageBoxButton.YesNo,MessageBoxImage.Question))
            {
                TaskCount = 1;
                IsAbort = true;
                this.progressBar1.Value = 0;
                this.progressBar2.Value = 0;
                this.Hide();
            }
        }

        internal void UpdateSubProgress(string msgText,double percent)
        {
            if(!IsAbort)
            {
                UpdateProgressDelegate updatelblDelegate = new UpdateProgressDelegate(lblSubInfo.SetValue);
                Dispatcher.Invoke(updatelblDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.Label.ContentProperty, msgText });

                UpdateProgressDelegate updatePbDelegate = new UpdateProgressDelegate(progressBar2.SetValue);
                Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, percent });
            }
        }

        internal void UpdateSubProgress(string msgText)
        {
            if (!IsAbort)
            {
                UpdateProgressDelegate updatelblDelegate = new UpdateProgressDelegate(lblSubInfo.SetValue);
                Dispatcher.Invoke(updatelblDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.Label.ContentProperty, msgText });
            }
        }

        internal void UpdateSubProgress(double percent)
        {
            if (!IsAbort)
            {

                UpdateProgressDelegate updatePbDelegate = new UpdateProgressDelegate(progressBar2.SetValue);
                Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, percent });
            }
        }

        internal void UpdateMainProgress(string msgText, double percent)
        {
            if (!IsAbort)
            {
                UpdateProgressDelegate updatelblDelegate = new UpdateProgressDelegate(lblMainInfo.SetValue);
                Dispatcher.Invoke(updatelblDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.Label.ContentProperty, msgText });

                UpdateProgressDelegate updatePbDelegate = new UpdateProgressDelegate(progressBar1.SetValue);
                Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, percent });
            }
        }

        internal void UpdateMainProgress(string msgText)
        {
            if (!IsAbort)
            {
                UpdateProgressDelegate updatelblDelegate = new UpdateProgressDelegate(lblMainInfo.SetValue);
                Dispatcher.Invoke(updatelblDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.Label.ContentProperty, msgText });
            }
        }

        internal void UpdateMainProgress(double percent)
        {
            if (!IsAbort)
            {
                UpdateProgressDelegate updatePbDelegate = new UpdateProgressDelegate(progressBar1.SetValue);
                Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, percent });
            }
        }

        internal void FinishMainProgress()
        {
            UpdateProgressDelegate updatePbDelegate = new UpdateProgressDelegate(progressBar1.SetValue);
            Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, 100.0 });
        }

        public static void OpenUI()
        {
            if (rstnDlg == null)
            {
                rstnDlg = new DoubleCompletionBar();
                rstnDlg.IsShowDoubleProgress = false;
                rstnDlg.IsEnableAbort = false;
            }
            rstnDlg.Show();
        }

        public static void UpdateUI(string msgText, double percentComplete)
        {
            if (rstnDlg == null || !rstnDlg.IsVisible)
                return;
            rstnDlg.UpdateSubProgress(msgText, percentComplete);
        }

        public static void UpdateUI(string msgText)
        {
            if (rstnDlg == null || !rstnDlg.IsVisible)
                return;
            rstnDlg.UpdateSubProgress(msgText);
        }

        public static void UpdateUI(double percentComplete)
        {
            if (rstnDlg == null || !rstnDlg.IsVisible)
                return;
            rstnDlg.UpdateSubProgress(percentComplete);
        }

        public static void CloseUI()
        {
            IsRunning = TaskCount>1;
            if (rstnDlg == null)
                return;
            rstnDlg.progressBar2.Value = 0;
            if(!IsRunning)
                rstnDlg.Hide();
            if(IsRunning && IsAutoIncrease)
            {
                double percent = rstnDlg.progressBar1.Value + 100 / TaskCount;
                if (percent > 100)
                    percent = 100.0;
                rstnDlg.UpdateSubProgress(100);
                rstnDlg.UpdateMainProgress(percent);
            }
        }

        /// <summary>
        /// 显示任务处理进度
        /// </summary>
        /// <param name="mTaskCount">任务数,只有1个任务时,启动单进度条,多个任务启动双进度条</param>
        /// <param name="isAutoInCrease">小进度条完成后，自动更新大进度条</param>
        /// <param name="isEnableCancel">任务数,只有1个任务时,启动单进度条,多个任务启动双进度条</param>
        public static  void OpenTaskProgress(int mTaskCount,bool isAutoIncrease,bool isEnableAbort)
        {
            TaskCount = mTaskCount;
            IsAutoIncrease = isAutoIncrease;
            if (rstnDlg == null)
                rstnDlg = new DoubleCompletionBar();
            rstnDlg.IsShowDoubleProgress = TaskCount>1;
            rstnDlg.IsEnableAbort = isEnableAbort;
            rstnDlg.Show();
        }

        public static void UpdateTaskProgress(string msgText, double percentComplete)
        {
            if (rstnDlg == null || !rstnDlg.IsVisible)
                return;
            if (!IsAutoIncrease)
                rstnDlg.UpdateMainProgress(msgText, percentComplete);
            else //自动推进进度时,进度自行控制
                rstnDlg.UpdateMainProgress(msgText);
        }

        public static void UpdateTaskProgress(string msgText)
        {
            if (rstnDlg == null || !rstnDlg.IsVisible)
                return;
            rstnDlg.UpdateMainProgress(msgText);
        }

        public static void UpdateTaskProgress(double percentComplete)
        {
            if (rstnDlg == null || !rstnDlg.IsVisible)
                return;
            if (IsAutoIncrease)
                return;
            rstnDlg.UpdateMainProgress(percentComplete);
        }

        public static void CloseTaskProgress()
        {
            IsAutoIncrease = false;
            TaskCount = 1;
            IsRunning = false;
            if (rstnDlg != null)
            {
                rstnDlg.FinishMainProgress();
                System.Threading.Thread.Sleep(50);
                //rstnDlg.progressBar1.Value =100;
                rstnDlg.Hide();
                rstnDlg.progressBar1.Value = 0;
                rstnDlg.progressBar2.Value = 0;
            }
        }
    }
}
